<?php 

class MW_Simplenews_Adminhtml_NewsController extends Mage_Adminhtml_Controller_Action
{
	public  function indexAction()
	{
		$this->loadLayout()->renderLayout();
	}

	public function newAction()
	{
		$this->_forward('edit');
	}

	public function editAction()
	{	
		$id     = $this->getRequest()->getParam('id');
		$model  = Mage::getModel('simplenews/news')->load($id);
		if ($model->getId() || $id == 0) {
			$data = Mage::getSingleton('adminhtml/session')->getFormData(true);

			if (!empty($data)) {
				$model->setData($data);
			}

			Mage::register('simplenews_data', $model);

			$this->loadLayout();
			$this->_setActiveMenu('simplenews/simplenews');

			$this->_addBreadcrumb(Mage::helper('adminhtml')->__('News Manager'), Mage::helper('adminhtml')->__('News Manager'));
			// $this->_addBreadcrumb(Mage::helper('adminhtml')->__('Item News'), Mage::helper('adminhtml')->__('Item News'));

			$this->getLayout()->getBlock('head')->setCanLoadExtJs(true);
			
			$this->_addContent($this->getLayout()->createBlock('simplenews/adminhtml_news_edit'))
				 ->_addLeft($this->getLayout()->createBlock('simplenews/adminhtml_news_edit_tabs'));

			$this->renderLayout();

		} else {
			Mage::getSingleton('adminhtml/session')->addError(Mage::helper('simplenews')->__('Item does not exist'));
			$this->_redirect('*/*/');
		}		
	}

	public function saveAction() {
		if ($data = $this->getRequest()->getPost()) {
			if(isset($_FILES['images']['name']) && $_FILES['images']['name'] != '') {
				try {	
						/* Starting upload */	
						$uploader = new Varien_File_Uploader('images');
						
						// Any extention would work
		           		$uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
						$uploader->setAllowRenameFiles(false);
						
						// Set the file upload mode 
						// false -> get the file directly in the specified folder
						// true -> get the file in the product like folders 
						//	(file.jpg will go in something like /media/f/i/file.jpg)
						$uploader->setFilesDispersion(false);
								
						// We set media as the upload dir
						//$path = Mage::getBaseDir('skin') . DS .'frontend'. DS .'default'. DS .'mw_simplenews'. DS .'images';
						$path = Mage::getBaseDir('media') . DS .'mw_simplenews' ;
						$uploader->save($path, $_FILES['images']['name'] );
						
					} catch (Exception $e) {
						
		        	}
	        
		        //this way the name is saved in DB
	  			$data['images'] = $_FILES['images']['name'];
		        // $data['images'] = 'mw_simplenews/'.$file_name;
			}else{

					// $temp = Mage::getModel('simplenews/news')
					$data['images'] = $data['images']['value'];
			}


			$model = Mage::getModel('simplenews/news');
			$model->setData($data)->setId($this->getRequest()->getParam('id'));
			

			try {
				$model->save();
				Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('simplenews')->__('Item was successfully saved'));
				Mage::getSingleton('adminhtml/session')->setFormData(false);

				if ($this->getRequest()->getParam('back')) {
					$this->_redirect('*/*/edit', array('id' => $model->getId()));
					return;
				}
				$this->_redirect('*/*/');
				return;
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                Mage::getSingleton('adminhtml/session')->setFormData($data);
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
                return;
            }
        }
        Mage::getSingleton('adminhtml/session')->addError(Mage::helper('simplenews')->__('Unable to find item to save'));
        $this->_redirect('*/*/');
	}

	public function deleteAction(){
		if( $this->getRequest()->getParam('id') > 0 ) {
			try {
				$model = Mage::getModel('simplenews/news');
				 
				$model->setId($this->getRequest()->getParam('id'))
					->delete();
					 
				Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Item was successfully deleted'));
				$this->_redirect('*/*/');
			} catch (Exception $e) {
				Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
				$this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
			}
		}
		$this->_redirect('*/*/');
	}

}	

